import cv2

cap = cv2.VideoCapture(0)
# *'mp4v'就是解包操作 等同于'm','p','4','v'
# fourcc = cv2.VideoWriter_fourcc(*'mp4v')\
# avi格式视频
fourcc = cv2.VideoWriter_fourcc(*'XVID')

# (640,480) 表示摄像头拍视频，这个大小搞错了也不行
# 主要是这个分辨率
# vw = cv2.VideoWriter('output.mp4', fourcc, 30, (640, 480))
vw = cv2.VideoWriter('output.avi', fourcc, 30, (640, 480))

while cap.isOpened():
    ret, frame = cap.read()
    if not ret:
        print('can not receive frame, Exiting...')
        break
    vw.write(frame)
    cv2.imshow('frame', frame)
    if cv2.waitKey(1000 // 30) == ord('q'):
        break
# 别忘了释放资源
cap.release()
# 释放VideoWriter
vw.release()
cv2.destroyAllWindows()
